package com.example.asus.yuejian_10.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.asus.yuejian_10.Config;
import com.example.asus.yuejian_10.MainActivity;
import com.example.asus.yuejian_10.R;
import com.example.asus.yuejian_10.RetrofitSeveice;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.example.asus.yuejian_10.base.BaseActivity.realname;
import static com.example.asus.yuejian_10.base.BaseActivity.usertel;

public class ApplyActivity extends AppCompatActivity {
    private TextView order_name;
    private TextView order_class;
    private TextView order_phone;
    private EditText ticket_number;
    private Button btn_jia;
    private Button btn_jian;
    private String id;
    private int uid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply);
        order_name= (TextView) findViewById(R.id.order_name);
        order_class= (TextView) findViewById(R.id.order_class);
        order_phone= (TextView) findViewById(R.id.order_number);
        ticket_number= (EditText) findViewById(R.id.ticket_number);
        btn_jia= (Button) findViewById(R.id.btn_jia);
        btn_jian= (Button) findViewById(R.id.btn_jian);

        String trainname = getIntent().getStringExtra("trainname");

        order_name.setText(realname);
        order_phone.setText(usertel);
        order_class.setText(trainname);


        MainActivity main = new MainActivity();
        if(main!=null){
            uid = Integer.parseInt(main.getUserId());
        }


        id = getIntent().getStringExtra("trainid");
        btn_jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int number = Integer.parseInt(String.valueOf(ticket_number.getText()));
                int num = number+1;
                ticket_number.setText(String.valueOf(num));

                //Toast.makeText(ApplyActivity.this,num,Toast.LENGTH_SHORT).show();
            }
        });

        btn_jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int number = Integer.parseInt(String.valueOf(ticket_number.getText()));
                int num = number-1;
                ticket_number.setText(String.valueOf(num));

                //Toast.makeText(ApplyActivity.this,num,Toast.LENGTH_SHORT).show();
            }
        });

        int number = Integer.parseInt(String.valueOf(ticket_number.getText()));

        int aaa = number*99;

        String pay = String.valueOf(aaa);

        Retrofit retrofit =
                new Retrofit.Builder()
                        .baseUrl(Config.URL)
                        .addConverterFactory(GsonConverterFactory.create()).build();
        RetrofitSeveice service = retrofit.create(RetrofitSeveice.class);

        Call<Integer> orderlist = service.signTrain(realname,id,usertel,String.valueOf(ticket_number.getText()),pay,uid,"2ul9sj8vlfoZ122e1s98048k2");

        orderlist.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                Integer number = response.body();

                if (number>0){
                    Toast.makeText(ApplyActivity.this,"报名成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(ApplyActivity.this,ApplyActivity.class);
                    startActivity(intent);
                }
                else {
                    Toast.makeText(ApplyActivity.this,"报名失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {

            }
        });

    }
}
